import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import time

class Testjob:

    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/1_get_job_list.yaml'))
    def test_get_job_list(self, jobinfo, get_env_info):
        # 获取job列表接口
        baseUrl = get_env_info
        url = baseUrl + jobinfo['request']['url'].replace('{namespace}','ingress-nginx')
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/2_create_job.yaml'))
    def test_create_job(self, jobinfo, get_env_info):
        # 创建job接口
        baseUrl = get_env_info
        url = baseUrl + jobinfo['request']['url'].replace('{namespace}','default')
        method = jobinfo['request']['method']
        data = jobinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in jobinfo['name']:
            jobname=req.json()['metadata']['name']
            globals()['jobname']=jobname
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/3_get_specific_job.yaml'))
    def test_get_specific_job(self, jobinfo, get_env_info):
        # 获取指定job接口
        baseUrl = get_env_info
        url = baseUrl + jobinfo['request']['url'].replace('{namespace}','default').replace('{jobname}',globals()['jobname'])
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in jobinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/4_update_job.yaml'))
    def test_update_job(self, jobinfo, get_env_info):
        # 更新指定job接口
        baseUrl = get_env_info
        url = baseUrl + jobinfo['request']['url'].replace('{namespace}','default').replace('{jobname}',globals()['jobname'])
        method = jobinfo['request']['method']
        data = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/5_update_section_job.yaml'))
    def test_update_section_job(self, jobinfo, get_env_info):
        # 部分更新指定job的接口
        baseUrl = get_env_info
        url = baseUrl + jobinfo['request']['url'].replace('{namespace}','default').replace('{jobname}',globals()['jobname'])
        method = jobinfo['request']['method']
        data = jobinfo['request']['data']
        header = jobinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/6_delete_specific_job.yaml'))
    def test_delete_specific_job(self, jobinfo, get_env_info):
        # 删除job接口
        baseUrl = get_env_info
        url = baseUrl + jobinfo['request']['url'].replace('{namespace}','default').replace('{jobname}',globals()['jobname'])
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/job/7_get_pod_list.yaml'))
    def test_delete_pod_createby_job(self, podinfo, get_env_info):
        # 获取job任务生成的pod，并删除
        time.sleep(3)
        baseUrl = get_env_info
        url = baseUrl + podinfo['request']['url'].replace('{namespace}','default')
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        podlist = req.json()['items']
        for i in podlist:
            if 'librespeed-job' in i['metadata']['name']:
                podname=i['metadata']['name']
                del_url = url+'/' +podname.replace('{podname}',podname)
                req = RequestUtil().send_request('delete', del_url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']